﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RouterControl.IO.UPnP;
using RouterControl.Whs.Wizards;
using Microsoft.HomeServer.Controls;

namespace RouterControl.WhsAddIn.Wizards.SelectRouter
{
    public partial class PageRouterList : WizardPageClientBannerTop
    {
        private WizardMain ParentWizardMain
        {
            get { return (WizardMain)this.ParentWizard; }
        }

        public PageRouterList()
        {
            InitializeComponent();
        }

        private void PageShowErrors_OkButtonClick(object sender, EventArgs e)
        {
            this.ParentWizard.Close();
        }

        public override void StartRealWork()
        {
            base.StartRealWork();

            // gather information
            var info = this.ParentWizardMain.UsageInfo;

            info.ExitPage = GetType().Name;

            // load errors
            this.errors.SuspendLayout();

            this.errors.Items.Clear();

            bool devicesFound = false;

            foreach (var d in this.ParentWizardMain.FoundDevices)
            {
                devicesFound = true;

                // get usage info
                var ui = info.EnsureRouterInfo(d.CreateSchemaId(), true);

                ui.RouterName = d.FriendlyName;
                ui.RouterDescription = d.Description;
                ui.RouterModel = d.ModelName;
                ui.RouterModelNumber = d.ModelNumber;
                ui.Manufacturer = d.ManufacturerName;
                ui.ManufacturerUrl = d.ManufacturerURL;
                ui.ExitPage = GetType().Name;

                // copy error to usage info
                var err = d.GetImplementationError();

                if (err != null)
                {
                    ui.SupportedNative = false;
                    ui.ErrorCode = err.ErrorCode;
                    ui.ErrorMessage = err.Message;
                }
                else
                {
                    ui.SupportedNative = true;
                }

                // check if the device works correctly
                if (err == null)
                {
                    var i = MessageListBoxHelpers.CreateItem(
                        MessageListBoxHelpers.Severity.Success,
                        d.FriendlyName,
                        Resources.txtRouterFineForSelectionText,
                        Resources.txtRouterListSelectThisRouter
                        );

                    i.Description.Tag = d;
                    i.Description.LinkClicked += new LinkLabelLinkClickedEventHandler(SelectThisRouter_LinkClicked);

                    this.errors.Items.Add(i);
                }
                else if (err.IsUpnpImplementationBroken())
                {
                    var i = MessageListBoxHelpers.CreateItem(
                        MessageListBoxHelpers.Severity.Error,
                        d.FriendlyName,
                        Resources.txtBrokenUpnpImplementationDesc
                        );

                    this.errors.Items.Add(i);
                }
                else
                {
                    var i = MessageListBoxHelpers.CreateItem(
                        MessageListBoxHelpers.Severity.Error,
                        d.FriendlyName,
                        string.Format(Resources.txtUnknownErrorText, err.Message)
                        );

                    this.errors.Items.Add(i);
                }
            }

            // no devices found?
            if (!devicesFound)
            {
                var i = MessageListBoxHelpers.CreateItem(
                    MessageListBoxHelpers.Severity.Error,
                    Resources.txtNoRouterFoundTitle,
                    Resources.txtNoRouterFoundDesc
                    );

                this.errors.Items.Add(i);
            }

            this.errors.ResumeLayout();
        }

        private void SelectThisRouter_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {
            var router = (IUPnPDevice)((LinkLabel)sender).Tag;

            if (router == null)
                throw new Exception("router is null");

            this.ParentWizardMain.SelectedDevice = router;

            this.SetCurrentPage<PageFeatureTest>();
        }

        private void PageRouterList_BackButtonClick(object sender, EventArgs e)
        {
            this.SetCurrentPage<PageSearch>();
        }

    }
}